<template id='tagList'>
  <div class="tagList">
    <div class="tagTitle">{{ options.title }}</div>
    <div class="tagContent">
      <div class="tab">
        <div class="item" v-for="(item, index) in options.list" :key="index" v-if="index<=9">
          <p class="name hiden" :class="[{ isClick: item.id == currentClick }]" @click="clickItem(item, index)"
            :ref="'item' + item.id">
            {{ item.name }}
          </p>
          <div class="count disCenter">
            {{ item.count }}
          </div>
        </div>
        <transition name="tabpanel">
          <div class="panel" v-if="options.isShowTab && panelHight" :style="{'height':panelHight + 'px'}">
            <div class="item" v-for="(item, index) in options.list" :key="index" v-if="index>9">
              <p class="name hiden" :class="[{ isClick: item.id == currentClick }]" @click="clickItem(item, index)"
                :ref="'item' + item.id">
                {{ item.name }}
              </p>
              <div class="count disCenter">
                {{ item.count }}
              </div>
            </div>
          </div>
        </transition>
      </div>
    </div>
    <p class="tagAll">
      <span @click="showAll" v-if="options.showAll || hasShowAll">{{ btnName }}</span>
    </p>
    <p class="tagDesc" v-if="!options.list.length">{{ options.default }}</p>
  </div>
</template>

<script>
import { createNamespacedHelpers } from "vuex";
const home = createNamespacedHelpers("home");
// 这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
export default {
  name: "tagList",
  components: {},
  props: {
    options: {
      type: Object,
    },
    currentClick: {
      type: String,
    }
  },
  // 定义属性
  data() {
    return {
      btnName: "",
      hasShowAll: false,
      panelHight: 0,
    };
  },
  // 计算属性，会监听依赖属性值随之变化
  computed: {
    ...home.mapState(['tagsData']),
  },
  // 监控data中的数据变化
  watch: {
    options: {
      handler(newName, oldName) {
        if (newName.isShowTab) {
          this.btnName = 'hide';
        } else {
          this.btnName = 'Show all'
        }
      },
      immediate: true,
      deep: true,
    },
  },
  // 方法集合
  methods: {
    ...home.mapActions(['getLeftEntries']),
    clickItem(item, index) {
      this.$emit("click", item);
    },
    showAll() {

      if (this.options.isShowTab) {
        this.tagsData.type = 0;
        this.getLeftEntries()
      } else {
        this.tagsData.tagsList.map(item => {
          item.isShowTab = false
        })
        this.tagsData.type = this.options.id;
        this.getLeftEntries().then(res => {
          this.panelHight = (Object.keys(this.options.list).length - 9) * 26 + 10;
        });
      }
      this.tagsData.tagsList.map(item => {
        if (item.id == this.options.id) {
          item.isShowTab = !item.isShowTab
        }
      })
      this.hasShowAll = true;
    },

    initTag() {
      this.tagsData.tagsList.map(item => {
        if (item.id == this.options.id) {
          item.isShowTab = false
        }
      })
    },
  },
  // 生命周期 - 创建完成（可以访问当前this实例）
  created() { },
  // 生命周期 - 挂载完成（可以访问DOM元素）
  mounted() { },
  beforeCreate() { }, // 生命周期 - 创建之前
  beforeMount() { }, // 生命周期 - 挂载之前
  beforeUpdate() { }, // 生命周期 - 更新之前
  updated() { }, // 生命周期 - 更新之后
  beforeDestroy() { }, // 生命周期 - 销毁之前
  destroyed() { }, // 生命周期 - 销毁完成
  activated() { }, // 如果页面有keep-alive缓存功能，这个函数会触发
};
</script>

<style lang='less' scoped>
.tagList {
  margin-bottom: 30px;

  .tagTitle {
    color: @tColor;
    font-size: 14px;
  }

  .tagContent {
    margin-top: 15px;

    .tab {
      padding-right: 2px;

      .item {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 10px;

        .name {
          cursor: pointer;
          width: 151px;
          font-size: 12px;
          color: @dColor;
        }

        .isClick {
          color: @tColor;
          -webkit-animation: chose-tag 0.5s both;
          animation: chose-tag 0.5s both;
        }

        .count {
          border-radius: 9px;
          background-color: @tColor;
          width: 28px;
          height: 16px;
          color: #ffffff;
          font-size: 10px;
        }
      }

      .item:last-child {
        margin-bottom: 0;
      }

      .tabpanel-enter-active,
      .tabpanel-leave-active {
        transition: all 0.2s cubic-bezier(0.25, 0.46, 0.45, 0.94);
        opacity: 1;
        -webkit-filter: blur(1px);
        filter: blur(1px);
      }

      .tabpanel-enter,
      .tabpanel-leave-to {
        height: 0 !important;
        opacity: 0.4;
        -webkit-filter: blur(0px);
        filter: blur(0px);
      }
    }
  }

  .tagAll {
    font-size: 12px;
    color: @tColor;
    margin: 17px auto;
    text-align: center;

    span {
      cursor: pointer;
    }
  }

  .tagDesc {
    font-size: 12px;
    color: @dColor;
    line-height: 1.2;
  }
}

@media screen and (max-width: 650px) {
  .tagList {
    .tagTitle {
      font-size: 15px;
    }

    .tagContent {
      .tab {
        .item {
          .name {
            width: 130px;
            font-size: 14px;
          }

          .count {
            font-size: 12px;
          }
        }
      }
    }
  }
}
</style>